<template>
	<view class="box">
		<u-loading-page :loading="loadingShow" bgColor="#ffffff94;" loadingColor="#1abc9c;" color="#1abc9c;"
			loadingText="登陆中......" fontSize="30rpx"></u-loading-page>
		<view class="logo">
			<image src="https://www.tdtye.com/files/vegetable_mall_file/images/loginLoad.png" mode=""></image>
		</view>
		<view class="btnChat">
			<button type="default" @tap="goto" :disabled="btn">一键登录</button>
			<view class="desc">登录代表您已同意
				<text style="color: #1b883787;" @click="goPage">《用户协议》</text>
			</view>
		</view>
	</view>
</template>

<script>
	import rsa from '@/utils/rsa.js'
	export default {

		data() {
			return {
				loadingShow: false,
				btn: false,
				avatarUrl: '',
				nickName: '请登录',
				sessionKey: '',
				api: '',
				imgSrc: "../../static/images/mine/icon_next.png"
			}
		},
		onLoad(e) {
			let token = uni.getStorageSync("token")
			if (e.parentId == "" && e.parentId == undefined) {
				if (token) {
					uni.switchTab({
						url: "../shop/shop"
					})
				}
			}
			this.api = this.$reqConfig.UPLOADAPI
		},
		methods: {
			goPage() {
				uni.navigateTo({
					url:"../../pageA/mine/agreement"
				})
			},
			goto() {
				this.btn = true
				this.loadingShow = true
				//登录				
				wx.login({
					success: res => {
						// 发送 res.code 到后台换取 openId, sessionKey, unionId
						if (res.code) {
							console.log("成功获取到code")
							let code = res.code;
							wx.setStorage({
								key: "code",
								data: code
							})
							//发起网络请求
							let that = this
							wx.request({
								method: "GET",
								url: this.api + '/wx/ma/user/public/login',
								data: {
									"code": code,
									"parentId": uni.getStorageSync("parentId")
								},
								header: {
									'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
								},
								success(res) {
									that.sessionKey = res.data.sessionKey;
									if (res.data.token) {
										uni.setStorageSync('token', res.data.token)
									}
									if(res.data.parentAvatar){
										uni.setStorageSync("parentAvatar",res.data.parentAvatar);
										uni.setStorageSync("parentNick",res.data.parentNick);
									}
								},
							})
						}
					},
					complete: info => {
						this.btn = false
					}
				})
				let that = this
				wx.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (res) => {
						this.$axios({
							url: "/wx/ma/user/info",
							data: {
								"encryptedData": res.encryptedData,
								"iv": res.iv,
								"rawData": res.rawData,
								"signature": res.signature,
								"sessionKey": rsa.encryptByPublicKey(that.sessionKey)
							}
						}).then((res) => {
							this.iphone = res.result.phone
							uni.setStorageSync('iphone', res.result.phone);
							uni.setStorageSync('usId', res.result.id);
							if (res.result.merchantId != null) {
								
								uni.setStorageSync('merchantId', res.result.merchantId);
							}
							uni.showLoading({
								title: '加载中'
							});
							let pages = getCurrentPages();
							if (pages[pages.length - 2]) {
								//如果有上一页，就返回上一页																
								uni.navigateBack({	
									delta: 1,
									complete: () => {
										//如果返回了还是登录页那么直接退回到首页
										let pages = getCurrentPages()
										console.log("pagee", pages[pages.length - 1].route)
										if (pages[pages.length - 1].route == "pages/mine/loginPage") {
											console.log("返回多个层级")
											uni.navigateBack({
												delta: 10,
												complete: (err) => {
													console.log("err: ",err);
												}
											})
										}else{
											console.log("返回单个层级");
											uni.redirectTo({
												url:pages[pages.length - 1].route
											})
											// uni.navigateBack({
											// 	delta: 1,
											// 	complete: (err) => {
											// 		console.log("err: ",err);
											// 	}
											// })
										}
									}
								})
							} else {
								console.log("跳转到首页");
								uni.switchTab({
									url: '../growVegetables/growVegetables'
								})
							}
							uni.hideLoading()
						})


						this.avatarUrl = res.userInfo.avatarUrl
						this.nickName = res.userInfo.nickName
						wx.setStorage({
							key: 'nickName',
							data: res.userInfo.nickName
						})
						wx.setStorage({
							key: 'avatarUrl',
							data: res.userInfo.avatarUrl
						})
						//存储对象
						uni.setStorage({
							key: 'infoObj',
							data: {
								"encryptedData": res.encryptedData,
								"iv": res.iv,
								"rawData": res.rawData,
								"signature": res.signature,
								"sessionKey": this.sessionKey
							}
						});
					},
					fail: (err) => {
						uni.showToast({
							icon: 'none',
							duration: 2000,
							title: "请授权登录小程序以获得完整的体验"
						})
					},
					complete: () => {
						this.loadingShow = false
					}
				})
			}
		}
	}
</script>

<style scoped>
	.box {
		display: flex;
		flex-direction: column;
		justify-content: space-around;
		align-items: center;
		height: 100vh;
		background-image: linear-gradient(#1bb75ac7, #c2ff81);
	}

	.back image {
		height: 100%;
	}

	.logo {
		width: 300rpx;
		height: 300rpx;
	}

	.logo image {
		width: 100%;
		height: 100%;
	}

	.btnChat {
		margin-bottom: 760rpx;
	}

	.btnChat button {
		width: 530rpx;
		margin-bottom: 20rpx;
		border-radius: 100rpx;
		color: #7dd44d;
		font-weight: 900;
	}

	.desc {
		font-size: 30rpx;
		color: #fff;
		text-align: center;
		font-weight: 600;
	}
</style>
